package com.javaProject.controller;

import com.javaProject.dto.R;
import com.javaProject.entity.Bishe;
import com.javaProject.entity.Student;
import com.javaProject.entity.Xuanze;
import com.javaProject.service.BisheService;
import com.javaProject.service.StudentService;
import com.javaProject.service.XuanzeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;


@CrossOrigin
@Tag(name = "课设双向选择")
@RestController
public class xuanzeController {

    @Autowired
    private XuanzeService xuanzeService;

    @Autowired
    private BisheService bisheService;

    @Autowired
    private StudentService studentService;

    @Operation(summary = "获取老师课设数据",description = "需要老师id")
    @Parameter(name = "teacher_id",description = "教师id",deprecated = true)
    @PostMapping("/xuanze/getStudentXuanze")
    public List<Object> getStudentXuanze(@RequestBody Xuanze xuanze) throws NoSuchAlgorithmException, InvalidKeySpecException {
        List<Object> bishelist = new ArrayList<>();
        List<Xuanze> xuanzes = new ArrayList<>();
        List<Student> stulist = new ArrayList<>();
        Bishe bishe = new Bishe();
        bishe.setTeacherId(xuanze.getTeacherId());
        List<Bishe> bishes = bisheService.getTeacherBishe(bishe);
        for (Bishe bishe1:bishes){
            xuanze.setBisheId(bishe1.getBisheId());
            xuanzes = xuanzeService.getStudentXuanze(xuanze);
            for (Xuanze xuanze1 : xuanzes){
                   stulist.add(studentService.getOneStudent(xuanze1.getStudentId()));
            }
            //System.out.println(stulist);
            bishe1.setStudent(stulist);
            bishelist.add(bishe1);

            stulist = new ArrayList<>();
        }
        System.out.println(bishelist);
        return bishelist;
    }


    @PostMapping("/xuanzhe/getStudentBishe")
    @Operation(summary = "获取学生个人选择毕设",description = "传入student_id")
    public R getStudentBishe(@RequestBody Xuanze xuanze){
        Integer bisheId = xuanzeService.getStudentBishi(xuanze);
        if (bisheId == null){
            return R.fail().setMsg("未选择");
        }
        return R.ok().setData(bisheService.getOneBIshe(bisheId));
    }

    @PostMapping("/xuanze/add")
    @Operation(summary = "新增选择",description = "需要学生id，毕设id，teacherid")
    public R add(@RequestBody Xuanze xuanze){
        return xuanzeService.add(xuanze);
    }


}
